package com.gdth.widget.calendar;

import android.content.Context;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.gdth.R;
import com.gdth.widget.calendar.CalendarView.OnCellClickListener;

public class Calendar extends LinearLayout implements OnCellClickListener{
	/**
	 * 分页控件
	 */
	ViewPager mViewPager;
	
	/**
	 * 顶部日期
	 */
	TextView monthText;
	
	/**
	 * 控件左右切换按钮
	 */
	ImageButton preImgBtn, nextImgBtn, preImgYear, nextImgYear;
	
	private int changeState = 1;
	private int mCurrentIndex = 498;
	
	/**
	 * 数据源
	 */
	private CalendarView[] mShowViews;
	
	/**
	 * 数据适配器
	 */
	private CalendarViewAdapter<CalendarView> adapter;
	
	
	private CalendarSildeDirection mDirection = CalendarSildeDirection.NO_SILDE;
	
	/**
	 * 日期点击事件
	 */
	private onCalendarClickListener mOnCalendarClickListener;

	private Context mContext;
	
	public Calendar(Context mContext){
		super(mContext);
		
		this.mContext = mContext;
		
		initView();
	}
	
	/**
	 * 初始化
	 */
	private void initView(){
		LayoutInflater inflater=(LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        
		inflater.inflate(R.layout.system_calendar, this);
		
		mViewPager = (ViewPager) findViewById(R.id.vp_calendar);
		monthText = (TextView) findViewById(R.id.tvCurrentMonth);
		preImgBtn = (ImageButton) findViewById(R.id.btnPreMonth);
		nextImgBtn = (ImageButton) findViewById(R.id.btnNextMonth);
		preImgYear = (ImageButton) findViewById(R.id.btnPreYear); 
		nextImgYear = (ImageButton) findViewById(R.id.btnNextYear);

		preImgBtn.setOnClickListener(new OnMouthChanger(-1));
		nextImgBtn.setOnClickListener(new OnMouthChanger(1));
		preImgYear.setOnClickListener(new onYearChanger(-1));
		nextImgYear.setOnClickListener(new onYearChanger(1));
		
		CalendarView[] views = new CalendarView[3];
		for (int i = 0; i < 3; i++) {
			CalendarView mCalendarView = new CalendarView(this.mContext, this);
			mCalendarView.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
			views[i] = mCalendarView;
		}
		adapter = new CalendarViewAdapter<CalendarView>(views);
		
		mViewPager.setAdapter(adapter);
		mViewPager.setCurrentItem(498);
		mViewPager.addOnPageChangeListener(new OnPageChanger());
	}
	
	/**
	 * 分页切换监听
	 * @author lenovo
	 *
	 */
	private class OnPageChanger implements OnPageChangeListener{

		@Override
		public void onPageScrollStateChanged(int arg0) {
			// TODO Auto-generated method stub
			
		}

		@Override
		public void onPageScrolled(int arg0, float arg1, int arg2) {
			// TODO Auto-generated method stub
			
//			changeState = 1;
		}

		@Override
		public void onPageSelected(int arg0) {
			// TODO Auto-generated method stub
			measureDirection(arg0);
			updateCalendarView(arg0);
		}
		
	}
	
	/**
	 * 月份改变事件
	 * @author lenovo
	 *
	 */
	private class OnMouthChanger implements View.OnClickListener{
		private int pager;
		
		public OnMouthChanger(int pager){
			this.pager = pager;
			changeState = 1;
		}

		@Override
		public void onClick(View arg0) {
			// TODO Auto-generated method stub
			mViewPager.setCurrentItem(mViewPager.getCurrentItem() + pager);
		}
	}
	
	/**
	 * 点击年份切换时间事件
	 * @author lenovo
	 *
	 */
	private class onYearChanger implements View.OnClickListener{
		private int year;
		
		public onYearChanger(int year){
			this.year = year;
			changeState = 2;
		}
		@Override
		public void onClick(View arg0) {
			// TODO Auto-generated method stub
			mViewPager.setCurrentItem(mViewPager.getCurrentItem() + year);
		}
	}

	
	/**
	 * 计算ViewPager滑动切换方向
	 * 
	 * @param arg0
	 */
	private void measureDirection(int arg0) {
		if (arg0 > mCurrentIndex) {
			mDirection = CalendarSildeDirection.RIGHT;

		} else if (arg0 < mCurrentIndex) {
			mDirection = CalendarSildeDirection.LEFT;
		}
		mCurrentIndex = arg0;
	}

	/**
	 * 更新日历视图
	 * @param arg0
	 */
	private void updateCalendarView(int arg0) {
		if (changeState == 1){
			mShowViews = adapter.getAllItems();
			if (mDirection == CalendarSildeDirection.RIGHT) {
				mShowViews[arg0 % mShowViews.length].rightSlide();
			} else if (mDirection == CalendarSildeDirection.LEFT) {
				mShowViews[arg0 % mShowViews.length].leftSlide();
			}
			mDirection = CalendarSildeDirection.NO_SILDE;
		}
		else if (changeState == 2){
			mShowViews = adapter.getAllItems();
			if (mDirection == CalendarSildeDirection.RIGHT) {
				mShowViews[arg0 % mShowViews.length].yearSlide(1);
			} else if (mDirection == CalendarSildeDirection.LEFT) {
				mShowViews[arg0 % mShowViews.length].yearSlide(-1);
			}
			mDirection = CalendarSildeDirection.NO_SILDE;
		}
		
		changeState = 1;
	}
	
	/********************OnCellClickListener接口**********************/
	@Override
	public void clickDate(CalendarItem date) {
		// TODO Auto-generated method stub
		if (this.mOnCalendarClickListener != null){
			String datetime = date.year + "-" + date.month + "-" + date.day;
			this.mOnCalendarClickListener.onSelectCalendarListener(datetime);
		}
	}

	@Override
	public void changeDate(CalendarItem date) {
		// TODO Auto-generated method stub
		if (date.month < 10)
			monthText.setText("0" + date.month + "月" + " " + date.year + "年");
		else
			monthText.setText(date.month + "月" + " " + date.year + "年");
	}
	
	public void setOnCalendarClickListener(onCalendarClickListener mOnCalendarClickListener){
		this.mOnCalendarClickListener = mOnCalendarClickListener;
	}
	
	public interface onCalendarClickListener{
		public void onSelectCalendarListener(String datetime);
	}
}
